package www.sofn.com.address;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.sofn.com.accounts.AccountsService;
import www.sofn.com.accounts.domain.vo.WeChartMemberInfoVO;
import www.sofn.com.address.domain.pojo.AddressPO;
import www.sofn.com.address.domain.vo.AddressVO;
import www.sofn.com.address.domain.vo.DefaultAddressVO;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressService {

    @Resource
    private AddressMapper mapper;

    @Resource
    private AccountsService accountsService;

    //获取当前用户下的所以收货地址
    public List<AddressVO> findAll() {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        final List<AddressPO> addressPOS = mapper.selectList(new LambdaQueryWrapper<AddressPO>().eq(AddressPO::getMemberId, personal.getId()));
        final List<AddressVO> result = addressPOS.stream().map(v -> {
            return BeanUtil.toBean(v, AddressVO.class);
        }).collect(Collectors.toList());
        return result;
    }

    //获取收货地址详情
    public AddressVO getById(String id) {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        final AddressPO addressPO = mapper.selectOne(new LambdaQueryWrapper<AddressPO>().eq(AddressPO::getId, id).eq(AddressPO::getMemberId, personal.getId()));
        return BeanUtil.toBean(addressPO, AddressVO.class);
    }

    //新增收货地址
    @Transactional
    public void insert(AddressPO po) {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        final Long AddressNum = mapper.selectCount(new LambdaQueryWrapper<AddressPO>().eq(AddressPO::getMemberId, personal.getId()).eq(AddressPO::getDefaultStatus, true));
        po.setMemberId(Long.valueOf(personal.getId()));
        po.setDefaultStatus(AddressNum == 0);
        mapper.insert(po);
    }

    //编辑收货地址
    @Transactional
    public void updateById(AddressPO po) {
        mapper.updateById(po);
    }

    //删除收货地址
    @Transactional
    public void delById(@NotNull Serializable id) {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        mapper.delete(new LambdaQueryWrapper<AddressPO>().eq(AddressPO::getId, id).eq(AddressPO::getMemberId, personal.getId()));
    }

    //修改收货地址默认状态
    @Transactional
    public void changeDefaultStatus(@NotBlank String id) {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        mapper.delDefaultStatusByMemberId(personal.getId());
        mapper.updateDefaultStatusByIdAndMemberId(id, personal.getId());
    }

    //获取默认收货地址
    public DefaultAddressVO getDefaultAddress() {
        final WeChartMemberInfoVO personal = accountsService.getPersonal();
        final AddressPO addressPO = mapper.selectOne(new LambdaQueryWrapper<AddressPO>().eq(AddressPO::getMemberId, personal.getId()).eq(AddressPO::getDefaultStatus, true));
        return BeanUtil.toBean(addressPO,DefaultAddressVO.class);
    }
}
